package com.yupi.springbootinit.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.constant.CommonConstant;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.mapper.InterfaceInfoMapper;
import com.yupi.springbootinit.model.dto.interfaceinfo.InterfaceInfoQueryRequest;
import com.yupi.springbootinit.model.dto.interfaceinfo.InterfaceInfoUpdateRequest;
import com.yupi.springbootinit.model.entity.InterfaceInfo;
import com.yupi.springbootinit.model.vo.InterfaceInfoVO;
import com.yupi.springbootinit.service.InterfaceInfoService;
import com.yupi.springbootinit.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Calcium
* @description 针对表【interface_info(接口信息)】的数据库操作Service实现
* @createDate 2025-03-26 13:54:53
*/
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
    implements InterfaceInfoService{

    /**
     * 获取分页查询Wrapper
     * @param queryRequest 前端传入的数据
     * @return 处理好的queryWrapper
     */
    @Override
    public @NotNull QueryWrapper<InterfaceInfo> getQueryWrapper(InterfaceInfoQueryRequest queryRequest) {
        if (queryRequest == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        Long id = queryRequest.getId();
        String name = queryRequest.getName();
        String description = queryRequest.getDescription();
        String url = queryRequest.getUrl();
        Integer status = queryRequest.getStatus();
        String method = queryRequest.getMethod();
        Long createUserId = queryRequest.getCreateUserId();
        Date createTime = queryRequest.getCreateTime();
        String sortField = queryRequest.getSortField();
        String sortOrder = queryRequest.getSortOrder();
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", queryRequest.getId());
        queryWrapper.eq(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
        queryWrapper.like(StringUtils.isNotBlank(url), "url", url);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.eq(StringUtils.isNotBlank(method), "method", method);
        queryWrapper.eq(createUserId != null, "create_user_id", createUserId);
        queryWrapper.eq(createTime != null, "create_time", createTime);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),sortOrder.equals(CommonConstant.SORT_ORDER_ASC),sortField);
        return queryWrapper;
    }

    //获取更新wrapper
    @Override
    public UpdateWrapper<InterfaceInfo> getUpdateWrapper(InterfaceInfoUpdateRequest updateRequest) {
        if (updateRequest == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        Long id = updateRequest.getId();
        String name = updateRequest.getName();
        String description = updateRequest.getDescription();
        String url = updateRequest.getUrl();
        String requestParams = updateRequest.getRequestParams();
        String requestHeader = updateRequest.getRequestHeader();
        String responseHeader = updateRequest.getResponseHeader();
        Integer status = updateRequest.getStatus();
        String method = updateRequest.getMethod();
        UpdateWrapper<InterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(id != null, "id", id);
        updateWrapper.set(StringUtils.isNotBlank(name), "name", name);
        updateWrapper.set(StringUtils.isNotBlank(description), "description", description);
        updateWrapper.set(StringUtils.isNotBlank(url), "url", url);
        updateWrapper.set(StringUtils.isNotBlank(requestParams), "request_params", requestParams);
        updateWrapper.set(StringUtils.isNotBlank(requestHeader), "request_header", requestHeader);
        updateWrapper.set(StringUtils.isNotBlank(responseHeader), "response_header", responseHeader);
        updateWrapper.set(status != null, "status", status);
        updateWrapper.set(StringUtils.isNotBlank(method), "method", method);
        return updateWrapper;
    }

    //转换脱敏
    @Override
    public InterfaceInfoVO getInterfaceInfoVO(InterfaceInfo interfaceInfo) {
        if (interfaceInfo == null) return null;
        InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
        BeanUtils.copyProperties(interfaceInfo,interfaceInfoVO);
        return interfaceInfoVO;
    }

    //脱敏接口信息
    @Override
    public List<InterfaceInfoVO> getInterfaceInfoVOList(List<InterfaceInfo> interfaceInfoList) {
        if(CollUtil.isEmpty(interfaceInfoList)){
            return new ArrayList<>();
        }
        return interfaceInfoList.stream().map(this::getInterfaceInfoVO).collect(Collectors.toList());
    }


}




